package com.kreao.korean.config;

import com.ulisesbocchio.jasyptspringboot.annotation.EnableEncryptableProperties;
import org.jasypt.encryption.StringEncryptor;
import org.jasypt.encryption.pbe.PooledPBEStringEncryptor;
import org.jasypt.encryption.pbe.config.SimpleStringPBEConfig;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

/**
 * Jasypt配置加密配置类
 * 用于加密application.yml中的敏感信息
 */
@Configuration
@EnableEncryptableProperties
public class JasyptConfig {

    /**
     * 加密密钥，从环境变量或启动参数获取
     * 生产环境必须通过环境变量设置：JASYPT_ENCRYPTOR_PASSWORD
     */
    @Value("${jasypt.encryptor.password:korean-learning-secret-2023}")
    private String encryptorPassword;

    /**
     * 配置字符串加密器
     */
    @Bean("jasyptStringEncryptor")
    public StringEncryptor stringEncryptor() {
        PooledPBEStringEncryptor encryptor = new PooledPBEStringEncryptor();
        SimpleStringPBEConfig config = new SimpleStringPBEConfig();
        
        // 加密密钥
        config.setPassword(encryptorPassword);
        
        // 加密算法
        config.setAlgorithm("PBEWITHHMACSHA512ANDAES_256");
        
        // 密钥获取迭代次数
        config.setKeyObtentionIterations("1000");
        
        // 连接池大小
        config.setPoolSize("1");
        
        // 盐值生成器
        config.setSaltGeneratorClassName("org.jasypt.salt.RandomSaltGenerator");
        
        // IV生成器
        config.setIvGeneratorClassName("org.jasypt.iv.RandomIvGenerator");
        
        // 字符串输出类型
        config.setStringOutputType("base64");
        
        encryptor.setConfig(config);
        return encryptor;
    }
} 